﻿using System;
using System.Linq;
using System.Collections.Generic;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Display;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;
using Windows.UI;
using Windows.UI.Input.Inking;
using System.Diagnostics;
using Windows.UI.Xaml.Media.Imaging;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace WRS
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class Write : Page
    {

        const uint SUPPORTEDCONTACTS = 5;
        const double STROKETHICKNESS = 5;
        uint numActiveContacts;
        private Queue<string> writeList = new Queue<string>();
        private Queue<string> writeList1 = new Queue<string>();
        private string writeWord;
        private string title1;
        private int score;
        //InkManager inkman = new InkManager();
        Dictionary<uint, Point?> contacts;


        public Write()
        {
            this.InitializeComponent();
            score = 0;
            numActiveContacts = 0;
            contacts = new Dictionary<uint, Point?>((int)SUPPORTEDCONTACTS);
            Scenario1OutputRoot.PointerPressed += new PointerEventHandler(Scenario1OutputRoot_PointerPressed);
            Scenario1OutputRoot.PointerMoved += new PointerEventHandler(Scenario1OutputRoot_PointerMoved);
            Scenario1OutputRoot.PointerReleased += new PointerEventHandler(Scenario1OutputRoot_PointerReleased);
            Scenario1OutputRoot.PointerExited += new PointerEventHandler(Scenario1OutputRoot_PointerReleased);

            writeList.Enqueue("cag");
            writeList.Enqueue("dog");
            writeList.Enqueue("rct");
            writeList.Enqueue("cat");
            writeList1.Enqueue("bet");
            writeList1.Enqueue("bat");
            writeList1.Enqueue("cap");
            writeList1.Enqueue("net");
            writeList1.Enqueue("cat");

        }

            //audioPlayer.SetSource = 
        

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        /// 


        void Scenario1OutputRoot_PointerReleased(object sender, PointerEventArgs e)
        {
            uint ptrId = e.GetCurrentPoint(sender as FrameworkElement).PointerId;
            if (contacts.ContainsKey(ptrId))
            {
                contacts[ptrId] = null;
                contacts.Remove(ptrId);
                --numActiveContacts;
            }
            e.Handled = true;
        }

        void Scenario1OutputRoot_PointerMoved(object sender, PointerEventArgs e)
        {
            Windows.UI.Input.PointerPoint pt = e.GetCurrentPoint(Scenario1OutputRoot);
            uint ptrId = pt.PointerId;

            if (contacts.ContainsKey(ptrId) && contacts[ptrId].HasValue)
            {
                Point currentContact = pt.Position;
                Point previousContact = contacts[ptrId].Value;
                if (Distance(currentContact, previousContact) > 4)
                {
                    Line l = new Line()
                    {
                        X1 = previousContact.X,
                        Y1 = previousContact.Y,
                        X2 = currentContact.X,
                        Y2 = currentContact.Y,
                        StrokeThickness = STROKETHICKNESS,
                        Stroke = new SolidColorBrush(Colors.Black),
                        StrokeEndLineCap = PenLineCap.Round
                    };

                    contacts[ptrId] = currentContact;
                    ((System.Collections.Generic.IList<UIElement>)Scenario1OutputRoot.Children).Add(l);
                }
            }

            e.Handled = true;
        }

        private double Distance(Point currentContact, Point previousContact)
        {
            return Math.Sqrt(Math.Pow(currentContact.X - previousContact.X, 2) + Math.Pow(currentContact.Y - previousContact.Y, 2));
        }

        void Scenario1OutputRoot_PointerPressed(object sender, PointerEventArgs e)
        {
            if (numActiveContacts >= SUPPORTEDCONTACTS)
            {
                // cannot support more contacts
                return;
            }
            Windows.UI.Input.PointerPoint pt = e.GetCurrentPoint(Scenario1OutputRoot);
            contacts[pt.PointerId] = pt.Position;
            e.Handled = true;
            ++numActiveContacts;
        }

        void Scenario1Reset(object sender, RoutedEventArgs e)
        {
            Scenario1Reset();
        }

        public void Scenario1Reset()
        {
            ((System.Collections.Generic.IList<UIElement>)Scenario1OutputRoot.Children).Clear();
            numActiveContacts = 0;
            if (contacts != null)
            {
                contacts.Clear();
            }
        }



        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            var title = e.Parameter;
            title1 = title.ToString();
        }
        private void btnForward_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(Read), title1);
        }
        private void btnBackward_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(Spelling), title1);
        }

        private void btnRecognize_Click(object sender, RoutedEventArgs e)
        {

            if (title1 == "Animals")
            {
                writeWord = writeList.Dequeue();
            }
            else if (title1 == "Sports")
            {
                writeWord = writeList1.Dequeue();

                if (writeWord == "bet")
                {
                    Scenario1Reset();
                    traceWrite();
                }
            }  
            txtblock.Text = writeWord;
            //score += 10;
            //txtScore.Text = "Score: " + score.ToString();
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            Scenario1Reset();
            txtblock.Text = "";
        }

        private void traceWrite()
        {
            Image imgB = new Image();
            imgB.Source = new BitmapImage(new Uri("ms-appx:/Trace/b.png", UriKind.Absolute));
            Canvas.SetTop(imgB, 100);
            Canvas.SetLeft(imgB, 80);
            Scenario1OutputRoot.Children.Add(imgB);

            Image imgA = new Image();
            imgA.Source = new BitmapImage(new Uri("ms-appx:/Trace/a.png", UriKind.Absolute));
            Canvas.SetTop(imgA, 100);
            Canvas.SetLeft(imgA, 320);
            Scenario1OutputRoot.Children.Add(imgA);

            Image imgT = new Image();
            imgT.Source = new BitmapImage(new Uri("ms-appx:/Trace/t.png", UriKind.Absolute));
            Canvas.SetTop(imgT, 100);
            Canvas.SetLeft(imgT, 500);
            Scenario1OutputRoot.Children.Add(imgT);
        }
    }
}
